package com.hyz.service.impl;

import client.LogClient;
import com.hyz.pojo.ResponseResult;
import com.hyz.pojo.assetRetire.AssetRetireCheck;
import com.hyz.pojo.assetRetire.AssetRetireResponse;
import com.hyz.service.OperationService;
import com.hyz.util.Conversion;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.w3c.dom.ls.LSException;
import pojo.log.IsComplete;
import pojo.log.assetRetire.AssetRetire;
import pojo.log.assetRetire.AssetRetireResultEnum;

import java.util.List;

@Service
public class OperationServiceImpl implements OperationService {
    @Autowired
    private LogClient logClient;

    @Autowired
    private Conversion conversion;

    @Override
    public ResponseResult<List<AssetRetireResponse>> opsGetAssetRetires() {
        // TODO: 获取用户id
        Long opsId = 1L;

        AssetRetire assetRetire = AssetRetire.builder()
                .result(AssetRetireResultEnum.CREATE)
                .isComplete(IsComplete.UNFINISHED).build();
        List<AssetRetire> assetRetires = logClient.getAssetRetires(assetRetire);
        List<AssetRetireResponse> responses = conversion.getAssetRetireResponse(assetRetires);
        return new ResponseResult<>(200,"成功",responses);
    }

    @Override
    public ResponseResult<List<AssetRetireResponse>> opsUpdateAssetRetires(AssetRetireCheck retireCheck) {
        Long id = retireCheck.getId();
        Boolean result = retireCheck.getResult();
        if (id == null || result == null) {
            return new ResponseResult<>(500, "参数缺失");
        }
        AssetRetire assetRetire = new AssetRetire();
        assetRetire.setId(id);
        assetRetire.setResult(result ?
                AssetRetireResultEnum.OPERATION_AGREE :
                AssetRetireResultEnum.FAILED);
        assetRetire.setIsComplete(result ?
                IsComplete.UNFINISHED :
                IsComplete.COMPLETE);

        if(logClient.updateAssetRetireById(assetRetire)){
            return opsGetAssetRetires();
        }
        return new ResponseResult<>(500, "操作失败，请稍后再尝试");
    }
}
